
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class HeroGame1 extends GameCanvas implements Runnable, CommandListener {

    private TiledLayer mGrassLayer;
    private TiledLayer mStone;
    private Hero mHero;
    private Opponent mSnail;
    private Opponent mEnemy;
    private LayerManager mLayerManager = new LayerManager();
    private Midlet midlet;
    private Form menu;
    private int upMove = 3;
    private int dowMove = 0;
    private int sx = 10;
    private int sy = 230;
    private int ocx = 10;
    private int ocy = 130;
    private int j = 0;
    private  int t = 0;
    private float syEnemy;
    private int smove;
    public AudioService audio;
    int score = 10000;


    HeroGame1(Midlet midlet, Form menu) {

        super(true);
        this.menu = menu;
        this.midlet = midlet;
        createGame();
        new Thread(this).start();
        audio = new AudioService("Nhac_nen_hero1.wav");
        audio.playFromResource();
    }

    private void createGround(Graphics g, Image im) {
        try {
            Image bacgroundIamge = Image.createImage("/grass.png");
            mGrassLayer = new TiledLayer(6, 9, bacgroundIamge, 40, 40);
            int[] map = {
                1, 2, 3, 4, 4, 4,
                6, 7, 8, 4, 4, 4,
                11, 12, 13, 4, 4, 4,
                4, 4, 4, 4, 4, 4,
                4, 4, 4, 4, 4, 4,
                4, 4, 4, 4, 4, 4,
                4, 4, 4, 4, 4, 4,
                4, 4, 4, 4, 4, 4,
                4, 4, 4, 4, 4, 4
            };
            for (int i = 0; i < map.length; i++) {
                int colum = i % 6;
                int row = (i - colum) / 6;
                mGrassLayer.setCell(colum, row, map[i]);
            }
            mLayerManager.append(mGrassLayer);
        } catch (Exception e) {
        }
        mLayerManager.paint(g, 0, 0);
    }

    private void createStone(Graphics g, Image im) {
        try {
            Image backgroundImage = Image.createImage("/background_1.png");
            mStone = new TiledLayer(8, 10, backgroundImage, 30, 30);
            int[] map = {
                40, 40, 40, 40, 40, 40, 40, 40,
                0, 0, 40, 0, 0, 0, 0, 40,
                0, 0, 0, 0, 0, 0, 0, 40,
                0, 0, 0, 0, 40, 0, 0, 40,
                40, 40, 0, 0, 40, 0, 0, 40,
                40, 0, 0, 0, 0, 0, 40, 40,
                40, 0, 0, 0, 0, 0, 0, 0,
                0, 0, 0, 0, 0, 40, 40, 40,
                0, 0, 0, 0, 0, 0, 0, 0,
                40, 40, 0, 0, 0, 0, 0, 0
            };
            for (int i = 0; i < map.length; i++) {
                int colum = i % 8;
                int row = (i - colum) / 8;
                mStone.setCell(colum, row, map[i]);
            }
            mLayerManager.append(mStone);
        } catch (Exception e) {
        }
        mLayerManager.append(mStone);
    }

    private void createHero(Graphics g) {
        try {
            Image im = Image.createImage("/Hero.png");
            mHero = new Hero(im, 23, 41);
            mHero.setFrame(mHero.framefomove);
            mHero.setPosition(30, 30);
            mHero.defineReferencePixel(12, 21);
            mLayerManager.append(mHero);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private void createSnail(Graphics g) {
        try {
            Image mImage = Image.createImage("/Fother-snail.png");
            mSnail = new Opponent(mImage, 50, 50);
            mSnail.startformove = 0;
            mSnail.frameformove = 8;
            mSnail.go = "left";
            mSnail.setPosition(sx, sy);
            mSnail.defineReferencePixel(25, 25);
            mLayerManager.append(mSnail);
        } catch (Exception e) {
        }
    }

    private void createEnemy(Graphics g) {
        try {
            Image im = Image.createImage("/enemy.png");
            mEnemy = new Opponent(im, 25, 25);
            mEnemy.go ="up";
            mEnemy.setPosition(150, 30);
            mEnemy.defineReferencePixel(13, 13);
            mLayerManager.append(mEnemy);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    private Opponent octopus;
    
    private void createOctopus(Graphics g) {
        try {
            Image im = Image.createImage("/octopus.png");
            octopus = new Opponent(im, 99, 99);
            octopus.go ="right";
            octopus.setPosition(30, 150);
            octopus.defineReferencePixel(49, 49);
            mLayerManager.append(octopus);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    

    private void createGame() {
        try {
            Graphics g = getGraphics();
            Image im = Image.createImage("/tile.png");
            createSnail(g);
            createEnemy(g);
            createOctopus(g);
            createHero(g);
            createStone(g, im);
            createGround(g, im);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void run() {
        while (true) {
            try {
                Graphics g = getGraphics();
                int ox = mHero.getX();
                int oy = mHero.getY();

                if (mHero.isHeroAlive) {
                    mHero.setPosition(ox, oy);
                    int key = getKeyStates();
                    if ((key & LEFT_PRESSED) != 0) {
                        mHero.setFrame(0 + mHero.framefomove++);
                        mHero.setTransform(Sprite.TRANS_NONE);
                        mHero.setPosition(mHero.getX() - 1, mHero.getY());
                        if (mHero.framefomove == 12) {
                            mHero.framefomove = 9;
                        }
                    } else if ((key & RIGHT_PRESSED) != 0) {
                        mHero.setFrame(0 + mHero.framefomove++);
                        mHero.setPosition(mHero.getX() + 1, mHero.getY());
                        mHero.setTransform(Sprite.TRANS_MIRROR);
                        if (mHero.framefomove == 12) {
                            mHero.framefomove = 9;
                        }
                    }

                    if ((key & UP_PRESSED) != 0) {
                        mHero.setFrame(0 + upMove++);
                        mHero.setPosition(mHero.getX(), mHero.getY() - 1);
                        mHero.setTransform(Sprite.TRANS_MIRROR);
                        if (upMove == 5) {
                            upMove = 3;
                        }
                    }


                    if ((key & DOWN_PRESSED) != 0) {
                        mHero.setFrame(0 + dowMove++);
                        mHero.setPosition(mHero.getX(), mHero.getY() + 1);
                        mHero.setTransform(Sprite.TRANS_MIRROR);
                        if (dowMove == 2) {
                            dowMove = 0;
                        }
                    }

                    if (mHero.collidesWith(mStone, true)) {
                        mHero.setPosition(ox, oy);
                    }
                    if (mHero.getX() < 0) {
                        mHero.setPosition(0, mHero.getY());
                    }

                    if (mHero.getX() > 215) {
                        mHero.setPosition(210, mHero.getY());
                    }

                    if (mHero.getY() > 265) {
                        mHero.setPosition(mHero.getX(), 265);
                    }

                    if (mHero.getY() < 0) {
                        mHero.setPosition(mHero.getX(), 0);
                    }
                    
                    if (mHero.getX() >210&&mHero.getY()>250) {
                        mHero.isTranmitted = false;
                    }
                    
                } else {
                    audio.stop();
                    midlet.createGameOver();
                    break;
                }
                System.out.println(mHero.getX());
                if (mHero.getX()>200) {
                    audio.stop();
                    midlet.createTranmitted();
                    break;
                }


                if (mSnail.go == "right") {
                    sx = sx + 1;
                    mSnail.startformove = 10;
                } else {
                    mSnail.startformove = 30;
                    sx = sx - 1;
                }
                if (sx > 110) {
                    mSnail.go = "left";
                }
                if (sx < 10) {
                    mSnail.go = "right";
                }
                
                mSnail.setPosition(sx, sy);
                mSnail.setFrame(mSnail.startformove + j++);

                if (j > mSnail.frameformove) {
                    j = 0;
                }
                
                //octopus
                
                if (octopus.go == "right") {
                    ocx = ocx + 1;
                    octopus.startformove = 0;
                } else {
                    octopus.startformove = 0;
                    ocx = ocx - 1;
                }
                if (ocx > 30) {
                    octopus.go = "left";
                }
                if (ocx < 0) {
                    octopus.go = "right";
                }
                
                octopus.setPosition(ocx, ocy);
                octopus.setFrame(octopus.frameformove + t++);

                if (t > 2) {
                    t = 0;
                }
                


                if (!"up".equals(mEnemy.go)) {
                    syEnemy = syEnemy - 0.7f;
                    mEnemy.setPosition(mEnemy.getX(), (int)syEnemy);
                } else {
                    syEnemy = syEnemy + 0.7f;
                    mEnemy.setPosition(mEnemy.getX(), (int)syEnemy);
                }
                if (mEnemy.getY() > 145) {
                    mEnemy.go = "down";
                }
                if (mEnemy.getY() < 30) {
                    mEnemy.go = "up";
                }
                if (smove == 1) {
                    smove = 0;
                } else {
                    smove = 1;
                }

                if (mHero.collidesWith(mSnail, true)) {
                    System.out.println("collide");
                    mHero.isHeroAlive = false;
                }
                
                if (mHero.collidesWith(mEnemy, true)) {
                    System.out.println("collide");
                    mHero.isHeroAlive = false;
                }
                
                if (mHero.collidesWith(octopus, true)) {
                    System.out.println("collide");
                    mHero.isHeroAlive = false;
                }

                mLayerManager.paint(g, 0, 0);
                
                g.setColor(255, 255, 255);
                g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_LARGE));
                g.drawString(String.valueOf(score), 20, 30, g.TOP|g.LEFT);
                score --;
                
                

                flushGraphics();
                Thread.sleep(7);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public void commandAction(Command c, Displayable d) {
    }
}